﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Beetle.Sl.EditorControl.Core;

namespace Beetle.Sl.EditorControl.Controls
{
    public class ColorSelectPanel : Control
    {
        private System.Collections.ObjectModel.ObservableCollection<ThemeColorInfo> _themeColors;
        private ICommand _selectColorCommand;
        private object selectColorCommandLock = new object();
        public event EventHandler SelectedValue;
        private ColorInfo selectColorTemp = null;

        public System.Collections.ObjectModel.ObservableCollection<ThemeColorInfo> ThemeColors
        {
            get
            {
                if (this._themeColors == null)
                {
                    this._themeColors = new System.Collections.ObjectModel.ObservableCollection<ThemeColorInfo>();
                }

                return this._themeColors;
            }
        }

        public ColorInfo[] StandardColors { get; set; }

        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value",
           typeof(string),
           typeof(ColorSelectPanel),
           new PropertyMetadata(null, new PropertyChangedCallback(ColorSelectPanel.OnValuePropertyChanged)));

        public string Value
        {
            get
            {
                return (string)this.GetValue(ColorSelectPanel.ValueProperty);
            }
            set
            {
                this.SetValue(ColorSelectPanel.ValueProperty, value);
            }
        }

        public ICommand SelectColorCommand
        {
            get
            {
                if (this._selectColorCommand == null)
                {
                    lock (this.selectColorCommandLock)
                    {
                        if (this._selectColorCommand == null)
                        {
                            this._selectColorCommand = new DelegateCommand(this.OnSelectColorCommand);
                        }
                    }
                }

                return this._selectColorCommand;
            }
        }

        public ColorSelectPanel()
        {
            this.InitBasicColorData();
            this.DefaultStyleKey = typeof(ColorSelectPanel);
        }

        private void InitBasicColorData()
        {
            // init theme color
            ThemeColorInfo tc1 = new ThemeColorInfo()
            {
                ColorString = "#FFFFFFFF",
                Visibility = System.Windows.Visibility.Collapsed
            };

            tc1.SubColors = new ColorInfo[]             
            {
                new ColorInfo()
                {
                    ColorString = "#FFF2F2F2",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,  
                new ColorInfo()
                {
                    ColorString = "#FFD8D8D8",
                    Visibility = System.Windows.Visibility.Collapsed
                },
                new ColorInfo()
                {
                    ColorString = "#FFBFBFBF",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,
                new ColorInfo()
                {
                    ColorString = "#FFA5A5A5",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,  
                new ColorInfo()
                {
                    ColorString = "#FF7F7F7F",
                    Visibility = System.Windows.Visibility.Collapsed
                }
            };

            ThemeColorInfo tc2 = new ThemeColorInfo()
            {
                ColorString = "#FF000000",
                Visibility = System.Windows.Visibility.Collapsed
            };

            tc2.SubColors = new ColorInfo[]             
            {
                new ColorInfo()
                {
                    ColorString = "#FF7F7F7F",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,  
                new ColorInfo()
                {
                    ColorString = "#FF595959",
                    Visibility = System.Windows.Visibility.Collapsed
                },
                new ColorInfo()
                {
                    ColorString = "#FF3F3F3F",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,
                new ColorInfo()
                {
                    ColorString = "#FF262626",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,  
                new ColorInfo()
                {
                    ColorString = "#FF0C0C0C",
                    Visibility = System.Windows.Visibility.Collapsed
                }
            };

            ThemeColorInfo tc3 = new ThemeColorInfo()
            {
                ColorString = "#FFEEECE1",
                Visibility = System.Windows.Visibility.Collapsed
            };

            tc3.SubColors = new ColorInfo[]             
            {
                new ColorInfo()
                {
                    ColorString = "#FFDDD9C3",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,  
                new ColorInfo()
                {
                    ColorString = "#FFC4BD97",
                    Visibility = System.Windows.Visibility.Collapsed
                },
                new ColorInfo()
                {
                    ColorString = "#FF938953",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,
                new ColorInfo()
                {
                    ColorString = "#FF494429",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,  
                new ColorInfo()
                {
                    ColorString = "#FF1D1B10",
                    Visibility = System.Windows.Visibility.Collapsed
                }
            };

            ThemeColorInfo tc4 = new ThemeColorInfo()
            {
                ColorString = "#FF1F497D",
                Visibility = System.Windows.Visibility.Collapsed
            };

            tc4.SubColors = new ColorInfo[]             
            {
                new ColorInfo()
                {
                    ColorString = "#FFC6D9F0",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,  
                new ColorInfo()
                {
                    ColorString = "#FF8DB3E2",
                    Visibility = System.Windows.Visibility.Collapsed
                },
                new ColorInfo()
                {
                    ColorString = "#FF548DD4",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,
                new ColorInfo()
                {
                    ColorString = "#FF17365D",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,  
                new ColorInfo()
                {
                    ColorString = "#FF0F243E",
                    Visibility = System.Windows.Visibility.Collapsed
                }
            };

            ThemeColorInfo tc5 = new ThemeColorInfo()
            {
                ColorString = "#FF4F81BD",
                Visibility = System.Windows.Visibility.Collapsed
            };

            tc5.SubColors = new ColorInfo[]             
            {
                new ColorInfo()
                {
                    ColorString = "#FFDBE5F1",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,  
                new ColorInfo()
                {
                    ColorString = "#FFB8CCE4",
                    Visibility = System.Windows.Visibility.Collapsed
                },
                new ColorInfo()
                {
                    ColorString = "#FF95B3D7",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,
                new ColorInfo()
                {
                    ColorString = "#FF366092",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,  
                new ColorInfo()
                {
                    ColorString = "#FF244061",
                    Visibility = System.Windows.Visibility.Collapsed
                }
            };

            ThemeColorInfo tc6 = new ThemeColorInfo()
            {
                ColorString = "#FFC0504D",
                Visibility = System.Windows.Visibility.Collapsed
            };

            tc6.SubColors = new ColorInfo[]             
            {
                new ColorInfo()
                {
                    ColorString = "#FFF2DCDB",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,  
                new ColorInfo()
                {
                    ColorString = "#FFE5B9B7",
                    Visibility = System.Windows.Visibility.Collapsed
                },
                new ColorInfo()
                {
                    ColorString = "#FFD99694",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,
                new ColorInfo()
                {
                    ColorString = "#FF953734",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,  
                new ColorInfo()
                {
                    ColorString = "#FF632423",
                    Visibility = System.Windows.Visibility.Collapsed
                }
            };

            ThemeColorInfo tc7 = new ThemeColorInfo()
            {
                ColorString = "#FF9BBB59",
                Visibility = System.Windows.Visibility.Collapsed
            };

            tc7.SubColors = new ColorInfo[]             
            {
                new ColorInfo()
                {
                    ColorString = "#FFEBF1DD",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,  
                new ColorInfo()
                {
                    ColorString = "#FFD7E3BC",
                    Visibility = System.Windows.Visibility.Collapsed
                },
                new ColorInfo()
                {
                    ColorString = "#FFC3D69B",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,
                new ColorInfo()
                {
                    ColorString = "#FF76923C",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,  
                new ColorInfo()
                {
                    ColorString = "#FF4F6128",
                    Visibility = System.Windows.Visibility.Collapsed
                }
            };

            ThemeColorInfo tc8 = new ThemeColorInfo()
            {
                ColorString = "#FF8064A2",
                Visibility = System.Windows.Visibility.Collapsed
            };

            tc8.SubColors = new ColorInfo[]             
            {
                new ColorInfo()
                {
                    ColorString = "#FFE5E0EC",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,  
                new ColorInfo()
                {
                    ColorString = "#FFCCC1D9",
                    Visibility = System.Windows.Visibility.Collapsed
                },
                new ColorInfo()
                {
                    ColorString = "#FFB2A2C7",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,
                new ColorInfo()
                {
                    ColorString = "#FF5F497A",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,  
                new ColorInfo()
                {
                    ColorString = "#FF3F3151",
                    Visibility = System.Windows.Visibility.Collapsed
                }
            };

            ThemeColorInfo tc9 = new ThemeColorInfo()
            {
                ColorString = "#FF4BACC6",
                Visibility = System.Windows.Visibility.Collapsed
            };

            tc9.SubColors = new ColorInfo[]             
            {
                new ColorInfo()
                {
                    ColorString = "#FFDBEEF3",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,  
                new ColorInfo()
                {
                    ColorString = "#FFB7DDE8",
                    Visibility = System.Windows.Visibility.Collapsed
                },
                new ColorInfo()
                {
                    ColorString = "#FF92CDDC",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,
                new ColorInfo()
                {
                    ColorString = "#FF31859B",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,  
                new ColorInfo()
                {
                    ColorString = "#FF205867",
                    Visibility = System.Windows.Visibility.Collapsed
                }
            };

            ThemeColorInfo tc10 = new ThemeColorInfo()
            {
                ColorString = "#FFF79646",
                Visibility = System.Windows.Visibility.Collapsed
            };

            tc10.SubColors = new ColorInfo[]             
            {
                new ColorInfo()
                {
                    ColorString = "#FFFDEADA",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,  
                new ColorInfo()
                {
                    ColorString = "#FFFBD5B5",
                    Visibility = System.Windows.Visibility.Collapsed
                },
                new ColorInfo()
                {
                    ColorString = "#FFFAC08F",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,
                new ColorInfo()
                {
                    ColorString = "#FFE36C09",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,  
                new ColorInfo()
                {
                    ColorString = "#FF974806",
                    Visibility = System.Windows.Visibility.Collapsed
                }
            };

            this.ThemeColors.Add(tc1);
            this.ThemeColors.Add(tc2);
            this.ThemeColors.Add(tc3);
            this.ThemeColors.Add(tc4);
            this.ThemeColors.Add(tc5);
            this.ThemeColors.Add(tc6);
            this.ThemeColors.Add(tc7);
            this.ThemeColors.Add(tc8);
            this.ThemeColors.Add(tc9);
            this.ThemeColors.Add(tc10);

            // init standard color
            this.StandardColors = new ColorInfo[] {  new ColorInfo()
                {
                    ColorString = "#FFC00000",
                    Visibility = System.Windows.Visibility.Collapsed
                } , new ColorInfo()
                {
                    ColorString = "#FFFF0000",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,new ColorInfo()
                {
                    ColorString = "#FFFFC000",
                    Visibility = System.Windows.Visibility.Collapsed
                }  , new ColorInfo()
                {
                    ColorString = "#FFFFFF00",
                    Visibility = System.Windows.Visibility.Collapsed
                } , new ColorInfo()
                {
                    ColorString = "#FF92D050",
                    Visibility = System.Windows.Visibility.Collapsed
                } , new ColorInfo()
                {
                    ColorString = "#FF00B050",
                    Visibility = System.Windows.Visibility.Collapsed
                } , new ColorInfo()
                {
                    ColorString = "#FF00B0F0",
                    Visibility = System.Windows.Visibility.Collapsed
                } ,new ColorInfo()
                {
                    ColorString = "#FF0070C0",
                    Visibility = System.Windows.Visibility.Collapsed
                }  , new ColorInfo()
                {
                    ColorString = "#FF002060",
                    Visibility = System.Windows.Visibility.Collapsed
                } , new ColorInfo()
                {
                    ColorString = "#FF7030A0",
                    Visibility = System.Windows.Visibility.Collapsed
                }  };
        }

        private void OnSelectColorCommand(object arg)
        {
            if (this.selectColorTemp != null)
            {
                if (this.selectColorTemp != arg)
                {
                    this.selectColorTemp.Visibility = System.Windows.Visibility.Collapsed;
                    this.selectColorTemp = (ColorInfo)arg;
                    this.selectColorTemp.Visibility = System.Windows.Visibility.Visible;
                }
            }
            else
            {
                this.selectColorTemp = (ColorInfo)arg;
                this.selectColorTemp.Visibility = System.Windows.Visibility.Visible;
            }

            this.Value = this.selectColorTemp.ColorString;
            this.RaiseSelectedValue();
        }

        private Color GetColorFromHexString(string s)
        {
            s = s.TrimStart(new char[] { '#' });
            byte a = System.Convert.ToByte(s.Substring(0, 2), 16);
            byte r = System.Convert.ToByte(s.Substring(2, 2), 16);
            byte g = System.Convert.ToByte(s.Substring(4, 2), 16);
            byte b = System.Convert.ToByte(s.Substring(6, 2), 16);

            return Color.FromArgb(a, r, g, b);
        }

        private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        { }

        private void RaiseSelectedValue()
        {
            if (this.SelectedValue != null)
            {
                this.SelectedValue(this, null);
            }
        }
    }
}
